<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="transactionTypeId">
        <el-select
          v-model="queryParams.transactionTypeId"
          placeholder="请选择订单类型"
          clearable
        >
          <el-option
            v-for="item in billTypeOptions"
            :key="item.type"
            :label="item.type"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option label="待审核" value="0"/>
          <el-option label="已撤回" value="1"/>
          <el-option label="已完成" value="2"/>
          <el-option label="已驳回" value="3"/>
          <el-option label="待付款" value="4"/>
        </el-select>
      </el-form-item>
      <el-form-item label="交易类型" prop="typeId">
        <el-select
          v-model="queryParams.typeId"
          placeholder="请选择交易类型"
          clearable
        >
          <el-option
            v-for="item in orderTypeOptions"
            :key="item.type"
            :label="item.type"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户名" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人" prop="reviewer">
        <el-input
          v-model="queryParams.reviewer"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="意见" prop="opinion">
        <el-input
          v-model="queryParams.opinion"
          placeholder="请输入意见"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input
          v-model="queryParams.notes"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="distinguish">
        <el-select v-model="queryParams.distinguish" placeholder="请选择用户类型" clearable>
          <el-option label="高报师" value="0"/>
          <el-option label="用户" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:goods:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:goods:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:goods:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" fixed />
      <el-table-column label="订单号" align="center" prop="orderNum" width="300" />
      <el-table-column label="订单类型" align="center" prop="transactionTypeId" width="100">
        <template slot-scope="scope">
          {{ getOrderType(scope.row.transactionTypeId) }}
        </template>
      </el-table-column>
      <el-table-column label="交易类型" align="center" prop="typeId" width="100">
        <template slot-scope="scope">
          {{ getTranType(scope.row.typeId) }}
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="num" width="100">
        <template slot-scope="scope">
          {{ scope.row.num ? scope.row.num : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="money" width="100">
        <template slot-scope="scope">
          {{ scope.row.money ? scope.row.money : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="代金券" align="center" prop="voucher" width="100">
        <template slot-scope="scope">
          {{ scope.row.voucher ? scope.row.voucher : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="userId" width="200" />
      <el-table-column label="审核人" align="center" prop="reviewer" width="100">
        <template slot-scope="scope">
          {{ scope.row.reviewer ? scope.row.reviewer : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="审核日期" align="center" prop="reviewerTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.reviewerTime ? parseTime(scope.row.reviewerTime, '{y}-{m}-{d}') : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="意见" align="center" prop="opinion" width="100">
        <template slot-scope="scope">
          {{ scope.row.opinion ? scope.row.opinion : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="notes" width="100">
        <template slot-scope="scope">
          {{ scope.row.notes ? scope.row.notes : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="随机字符串" align="center" prop="randomString" width="100">
        <template slot-scope="scope">
          {{ scope.row.randomString ? scope.row.randomString : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="订单支付信息" align="center" prop="paymentInformation" width="100">
        <template slot-scope="scope">
          {{ scope.row.paymentInformation ? scope.row.paymentInformation : '--' }}
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.payTime ? parseTime(scope.row.payTime, '{y}-{m}-{d}') : '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户类型" align="center" prop="distinguish" width="100">
        <template slot-scope="scope">
          {{ scope.row.distinguish == 0 ? '高报师' : '用户' }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state" width="100" fixed="right">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state===0" type="warning">待审核</el-tag>
          <el-tag v-if="scope.row.state===1" type="danger">已撤回</el-tag>
          <el-tag v-if="scope.row.state===2" type="success">已完成</el-tag>
          <el-tag v-if="scope.row.state===3" type="danger">已驳回</el-tag>
          <el-tag v-if="scope.row.state===4" type="warning">待付款</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/system/goods";
import {getDocumentType, getTransactionType} from "@/api/expert/expert";

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        typeId: null,
        openId: null,
        state: null,
        transactionTypeId: null,
        num: null,
        money: null,
        voucher: null,
        userId: null,
        reviewer: null,
        reviewerTime: null,
        opinion: null,
        notes: null,
        randomString: null,
        paymentInformation: null,
        payTime: null,
        distinguish: null,
        flag: null,
      },
      // 表单参数
      form: {},
      // 订单类型选项列表
      billTypeOptions: [],
      // 交易类型选项列表
      orderTypeOptions: [],
      // 表单校验
      rules: {
        orderNum: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        typeId: [
          { required: true, message: "单据类型id{在交易类型表里面}不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        transactionTypeId: [
          { required: true, message: "交易类型{在交易类型表里面}不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "金额不能为空", trigger: "blur" }
        ],
        voucher: [
          { required: true, message: "代金券不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        distinguish: [
          { required: true, message: "区分字段{0是卖给高报师的订单  1是卖给了用户的订单}不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDocumentType();
    this.getTransactionType();
  },
  methods: {
    //获取单个单据类型
    getOrderType(transactionTypeId){
      const cardType = this.billTypeOptions.find(type => type.id === transactionTypeId);
      return cardType ? cardType.type : '未知类型';
    },
    //获取单个交易类型
    getTranType(typeId){
      const cardType = this.orderTypeOptions.find(type => type.id === typeId);
      return cardType ? cardType.type : '未知类型';
    },
    //获取单据类型
    getDocumentType(){
      getDocumentType().then(response => {
        this.billTypeOptions = response.data;
      });
    },
    //获取交易类型
    getTransactionType(){
      getTransactionType().then(response => {
        this.orderTypeOptions = response.data;
      });
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNum: null,
        typeId: null,
        openId: null,
        state: null,
        transactionTypeId: null,
        num: null,
        money: null,
        voucher: null,
        userId: null,
        reviewer: null,
        reviewerTime: null,
        opinion: null,
        notes: null,
        randomString: null,
        paymentInformation: null,
        payTime: null,
        distinguish: null,
        updateTime: null,
        flag: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
